package com.ynding;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * <p> 计算器 </p>
 *
 * @author ynding
 * @version 2021/4/14
 **/
public class Caculater extends Applet {
    private NumPanel nums;
    private OperationPanel myoperation;
    static TextField display;
    static String s = new String();

    //初始化
    @Override
    public void init() {
        setLayout(new BorderLayout());
        setBackground(new Color(105, 105, 105));
        myoperation = new OperationPanel();
        myoperation.setFont(new Font("SansSerif", Font.BOLD, 14));
        add(BorderLayout.EAST, myoperation);
        nums = new NumPanel();
        nums.setFont(new Font("SansSerif", Font.BOLD, 14));
        add(BorderLayout.WEST, nums);
        display = new TextField();
        display.setEditable(false);
        display.setBackground(Color.yellow);
        display.setFont(new Font("SansSerif", Font.BOLD, 14));
        add(BorderLayout.NORTH, display);
    }
}

//定义NumPanel类
class NumPanel extends Panel implements ActionListener {
    static boolean point = false;
    static short count = 0;
    private Button b0 = new Button(" 7 "),
            b1 = new Button(" 8 "),
            b2 = new Button(" 9 "),
            b3 = new Button(" 4 "),
            b4 = new Button(" 5 "),
            b5 = new Button(" 6 "),
            b6 = new Button(" 1 "),
            b7 = new Button(" 2 "),
            b8 = new Button(" 3 "),
            b9 = new Button(" 0 "),
            bpoint = new Button(" . ");

    //构造函数
    public NumPanel() {
        b0.addActionListener(this);
        b1.addActionListener(this);
        b2.addActionListener(this);
        b3.addActionListener(this);
        b4.addActionListener(this);
        b5.addActionListener(this);
        b6.addActionListener(this);
        b7.addActionListener(this);
        b8.addActionListener(this);
        b9.addActionListener(this);
        bpoint.addActionListener(this);
        //实例化Panel对象
        Panel p1 = new Panel();
        p1.setLayout(new GridLayout(4, 3, 10, 10));
        p1.add(b0);
        p1.add(b1);
        p1.add(b2);
        p1.add(b3);
        p1.add(b4);
        p1.add(b5);
        p1.add(b6);
        p1.add(b7);
        p1.add(b8);
        p1.add(b9);
        p1.add(bpoint);
        add(p1);
    }

    //处理事件
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();

        if (source == b0) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 7;
            Caculater.display.setText(Caculater.s);
        } else if (source == b1) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 8;
            Caculater.display.setText(Caculater.s);
        } else if (source == b2) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 9;
            Caculater.display.setText(Caculater.s);
        } else if (source == b3) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 4;
            Caculater.display.setText(Caculater.s);
        } else if (source == b4) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 5;
            Caculater.display.setText(Caculater.s);
        } else if (source == b5) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 6;
            Caculater.display.setText(Caculater.s);
        } else if (source == b6) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 1;
            Caculater.display.setText(Caculater.s);
        } else if (source == b7) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 2;
            Caculater.display.setText(Caculater.s);
        } else if (source == b8) {
            count++;
            Caculater.s = Caculater.display.getText();
            Caculater.s = Caculater.s + 3;
            Caculater.display.setText(Caculater.s);

        } else if (source == b9) {
            if (count == 0) {
                Caculater.s = Caculater.display.getText();
                Caculater.s = Caculater.s + "0.";
                Caculater.display.setText(Caculater.s);
                count++;
            } else {
                Caculater.s = Caculater.display.getText();
                Caculater.s = Caculater.s + 0;
                Caculater.display.setText(Caculater.s);
                count++;
            }
        } else if (source == bpoint) {
            if (point == false) {
                count++;
                Caculater.s = Caculater.display.getText();
                Caculater.s = Caculater.s + ".";
                Caculater.display.setText(Caculater.s);
                point = true;
            } else {
                return;
            }
        }
    }
}

//定义OperationPanel类
class OperationPanel extends Panel implements ActionListener {
    Double i;
    double temp = 0.0;
    private short opera = 0;
    private short counter = 0;
    private double result = 0;
    private Button plus = new Button("  +  "),
            minus = new Button("  -  "),
            clear = new Button("  C  "),
            ac = new Button(" AC "),
            times = new Button("  X  "),
            divide = new Button("  /  "),
            equals = new Button("  =  "),
            sto = new Button(" STO "),
            rcl = new Button(" RCL "),
            square = new Button(" SQRT ");

    //构造函数
    public OperationPanel() {
        setLayout(new GridLayout(5, 2, 10, 10));
        add(clear);
        add(ac);
        add(sto);
        add(rcl);
        add(plus);
        add(minus);
        add(times);
        add(divide);
        add(square);
        add(equals);
        clear.addActionListener(this);
        ac.addActionListener(this);
        plus.addActionListener(this);
        minus.addActionListener(this);
        times.addActionListener(this);
        divide.addActionListener(this);
        equals.addActionListener(this);
        square.addActionListener(this);
        sto.addActionListener(this);
        rcl.addActionListener(this);
    }

    //加法运算
    public void addition() {
        if (counter == 0) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            counter++;
            opera = 1;
        } else if (counter != 0 && opera == 1) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            opera = 1;
        } else if (counter != 0 && opera == 2) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result - i.doubleValue();
            opera = 1;
        } else if (counter != 0 && opera == 3) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result * i.doubleValue();
            opera = 1;
        } else if (counter != 0 && opera == 4) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result / i.doubleValue();
            opera = 1;
        }
    }

    //减法运算
    public void minus() {
        if (counter == 0) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            counter++;
            opera = 2;
        } else if (counter != 0 && opera == 2) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result - i.doubleValue();
            opera = 2;
        } else if (counter != 0 && opera == 1) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            opera = 2;
        } else if (counter != 0 && opera == 3) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result * i.doubleValue();
            opera = 2;
        } else if (counter != 0 && opera == 4) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result / i.doubleValue();
            opera = 2;
        }
    }

    //乘法运算
    public void times() {
        if (counter == 0) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            counter++;
            opera = 3;
        } else if (counter != 0 && opera == 3) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result * i.doubleValue();
            opera = 3;
        } else if (counter != 0 && opera == 1) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            opera = 3;
        } else if (counter != 0 && opera == 2) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result - i.doubleValue();
            opera = 3;
        } else if (counter != 0 && opera == 4) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result / i.doubleValue();
            opera = 3;
        }

    }

    //除法运算
    public void divide() {
        if (counter == 0) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            counter++;
            opera = 4;
        } else if (counter != 0 && opera == 4) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result / i.doubleValue();
            opera = 4;
        } else if (counter != 0 && opera == 1) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            opera = 4;
        } else if (counter != 0 && opera == 2) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result - i.doubleValue();
            opera = 4;
        } else if (counter != 0 && opera == 3) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result * i.doubleValue();
            opera = 4;
        }
    }

    //等于号
    public void equals() {
        if (opera == 1) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result + i.doubleValue();
            Caculater.display.setText("" + result);
        } else if (opera == 2) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result - i.doubleValue();
            Caculater.display.setText("" + result);
        } else if (opera == 3) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result * i.doubleValue();
            Caculater.display.setText("" + result);
        } else if (opera == 4) {
            i = new Double(Caculater.display.getText());
            Caculater.display.setText("");
            result = result / i.doubleValue();
            Caculater.display.setText("" + result);
        }
        counter = 0;
        result = 0;
    }

    //清零
    public void clear() {
        plus.setEnabled(true);
        minus.setEnabled(true);
        times.setEnabled(true);
        divide.setEnabled(true);
        equals.setEnabled(true);
        ac.setEnabled(true);
        Caculater.display.setText("0.0");
        i = new Double(Caculater.display.getText());
        Caculater.display.setText("");
        NumPanel.point = false;
        result = 0.0;
        counter = 0;
        opera = 0;
    }

    //平方根运算
    public void square() {
        i = new Double(Caculater.display.getText());
        result = i.doubleValue();
        Caculater.display.setText("" + Math.sqrt(result));
        clear.addActionListener(this);
        plus.setEnabled(false);
        minus.setEnabled(false);
        times.setEnabled(false);
        divide.setEnabled(false);
        equals.setEnabled(false);
        ac.setEnabled(false);
    }

    public void ac() {
        NumPanel.point = false;
        Caculater.display.setText("");
    }

    public void sto() {
        i = new Double(Caculater.display.getText());
        temp = i.doubleValue();
    }

    public void rcl() {
        NumPanel.point = true;
        Caculater.display.setText("" + temp);
    }

    //事件处理
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if (source == plus) {
            NumPanel.count = 0;
            NumPanel.point = false;
            addition();
        }
        if (source == minus) {
            NumPanel.count = 0;
            NumPanel.point = false;
            minus();
        }
        if (source == times) {
            NumPanel.count = 0;
            NumPanel.point = false;
            times();
        }
        if (source == divide) {
            NumPanel.count = 0;
            NumPanel.point = false;
            divide();
        }
        if (source == equals) {
            NumPanel.point = true;
            equals();
        }
        if (source == clear) {
            NumPanel.count = 0;
            clear();
        }
        if (source == square) {
            NumPanel.point = true;
            square();
        }
        if (source == ac) {
            NumPanel.count = 0;
            ac();
        }
        if (source == sto) {
            sto();
        }
        if (source == rcl) {
            rcl();
        }
    }
}
